<?php
namespace Festivalcnadmin\Controller;
use Think\Controller;

class IndexController extends BaseController {


	public function __construct()
    {
    	parent::__construct();
        $isLogin = session('userinfo');
        if (!$isLogin) {
            $this->redirect('Login/index');
            return;
        } else {
            session('userinfo',$isLogin); //用户信息
        }
    }


	 /*
     * 内容详细列表显示
     */
    public function main(){
    	$artcle = D(C('TABLE_ARTICLE'));
		$where['state'] = 0;//状态0代表显示，1代表隐藏。
		$fstvid = I("get.fstvid");
    	if(empty($fstvid)){ 
		  $where['a.fstvid'] = 7;//默认为春节
		}else{ 
		  $where['a.fstvid'] = $fstvid;
		}
    	$count = $artcle->alias('a')->join('left join fvcn_holiday as h on h.fstvid = a.fstvid')
    			->join('left join fvcn_nettype as n on n.netid = a.netid')
    			->order('a.id desc')->where($where)->count();
    	//每页显示条数
    	$num = C('MAIN_NUM');
    	$page = new \Think\Page($count,$num);
    	$obj = pagestyle($page);
    	$show = $obj->show();
    	$result = $artcle->alias('a')->join('left join fvcn_holiday as h on h.fstvid = a.fstvid')
    			->join('left join fvcn_nettype as n on n.netid = a.netid')
    			->order('a.id desc')->where($where)->limit($obj->firstRow.','.$obj->listRows)->select();
    	$this->assign('list',$result);
    	$this->assign('page',$show);
    	$this->display('main');
    }

	 /*
     * 添加节日分类
     */
    public function addcolumn(){
    	
		if(IS_POST){
			$fstvobj = D(C('TABLE_HOLIDAY'));
			$fstvobj->fstvnm = I('post.fstv_name');
            $fstvobj->fstvsort = I('post.fstv_sort');
			$fstvobj->fstvdt = I('post.fstv_startdate');
			$fstvobj->fstvpicture = "http://h5.hrtv.cn".I('post.picture');
			$fstvobj->fstvbanner = "http://h5.hrtv.cn".I('post.fstvbanner');
			$fstvobj->createdate = date('y-m-d h:i:s',time());
			$res = $fstvobj->add();
		//	echo $fstvobj->getLastSql();die;
			if($res){
				$this->success('添加成功','columnList');
			}else{
				$this->error('添加失败','',1);
			}

		}else{ 
			$this->display();

		}
    }
     /*
     * 节日分类列表
     */
    public function columnList(){ 
    	$fstvobj = D(C('TABLE_HOLIDAY'));
    	$count = $fstvobj->count();
    	//每页显示条数
    	$num = C('MAIN_NUM');
    	$page = new \Think\Page($count,$num);
    	$obj = pagestyle($page);
    	$show = $obj->show();
    	$result = $fstvobj->order(array('fstvid'=>'DESC'))->select();
    	$this->assign('page',$show);
    	$this->assign('fstvlist',$result);
    	$this->display();
    } 

    /*
     * 跳转到节日修改页
     */
    public function editcolumn(){ 
    	$id = I('get.fstvid');
    	if(!empty($id)){
    			$m = D(C('TABLE_HOLIDAY'));
    		 	$where['fstvid'] = $id;
    			$result = $m->where($where)->find();
    			$this->assign('fstvrest',$result);
				$this->display();
    		 }else{
    		    $this->error('参数错误','',1);
    		}
     }
 	/*
     * 节日分类修改
     */
    public function editSaveColum(){ 
    	if(IS_POST){
    		$fid = I('post.fstvid');
    		$m = D(C('TABLE_HOLIDAY'));
			$m->fstvnm = I("post.fstv_name");
            $m->fstvsort = I("post.fstv_sort");
			$m->fstvdt = I("post.fstv_startdate");
			$m->createdate = date('y-m-d h:i:s',time());
			$st1 = I('post.picture');
			$st2 = 'h5.hrtv.cn';
			$st3 = I('post.fstvbanner');
			$st4 = 'h5.hrtv.cn';
			if(strpos($st1,$st2) === false){ //使用绝对等于
    			//不包含
				$m->fstvpicture = "http://".$_SERVER['HTTP_HOST'].I('post.picture');
			}elseif(strpos($st3,$st4) === false){ 
				$m->fstvbanner = "http://".$_SERVER['HTTP_HOST'].I('post.fstvbanner');
			}elseif(strpos($st1,$st2) === true){
			    //包含
			    $m->fstvpicture = I('post.picture');
			}elseif(strpos($st3,$st4) === true){ 
 				$m->fstvbanner = I('post.fstvbanner');
			}
			$result = $m->where(array('fstvid'=>$fid))->save();
    		if($result !== false){
    			$this->success('编辑节日分类成功','columnList');
    		}else{
    			$this->error('更新数据失败','',1);
    		}
    	} 
    	
    }

    /*
     * 节日分类删除
     */
    public function delecolumn(){ 
    	$id = I('get.fstvid');
    	if(!empty($id)){
    		$m =  D(C('TABLE_HOLIDAY'));
    		$where['fstvid'] = $id;
    		$result = $m->where($where)->delete();

    		if($result !== false){
    			$this->success('删除成功');
    		}else{
    			$this->error('删除失败','',1);
    		}
    	}else{
    		$this->error('参数错误','',1);
    	}

    }

	/*
     * 添加网络类型
     */
	public function addnetType(){ 

		if(IS_POST){
			$fstvobj = D(C('TABLE_NETTYPE'));
			$fstvobj->netname = I('post.type_name');
			//$fstvobj->fstvid = I('post.ftype');
			$fstvobj->createdate = date('y-m-d h:i:s',time());
			$fstvobj->updatedate = date('y-m-d h:i:s',time());
		
			//$where['fstvid'] = I('post.ftype');
			$where['netname'] = I('post.type_name');
    		$result = $fstvobj->where($where)->count();
    		if($result > 0){ 
    			$this->error('类型已存在,不要重复添加','',1);
    		}else{ 
    			$res = $fstvobj->add();
				if($res){
					$this->success('添加成功','typeList');
				}else{
					$this->error('添加失败','',1);
				}
    		}
			
		}else{ 
			//查询节日名称
			$info  = D(C('TABLE_HOLIDAY'));
	        $list = $info->select();
		    $this->assign('fstv',$list);
			$this->display();

		}

	}

	 /*
     * 网络类型列表
     */
    public function typelist(){ 
    	$typeobj = D(C('TABLE_NETTYPE'));
    	$count = $typeobj->count();
    	//每页显示条数
    	$num = C('MAIN_NUM');
    	$page = new \Think\Page($count,$num);
    	$obj = pagestyle($page);
    	$show = $obj->show();
    	//$result = $typeobj->alias('t')->join('fvcn_holiday as h on h.fstvid = t.fstvid')->order(array('netid'=>'DESC'))->select();
    	$result = $typeobj->order(array('updatedate'=>'DESC'))->select();
    	$this->assign('page',$show);
    	$this->assign('typelist',$result);
    	$this->display();
    } 

    /*
     * 跳转到类型修改页
     */
    public function edittype(){ 
	$id = I('get.netid');
	if(!empty($id)){
			$m = D(C('TABLE_NETTYPE'));
		 	$where['netid'] = $id;
//			$result = $m->alias('t')->join('fvcn_holiday as h on h.fstvid = t.fstvid')->where($where)->find();
			$result = $m->where($where)->find();
			$this->assign('netrest',$result);
			$this->assign('fvcnm',$this->selectfvcnm());
			$this->display();
		 }else{
		    $this->error('参数错误','',1);
		}	
    }
    /*
     * 查询节日名称
     */
     public function selectfvcnm(){ 
		$m = D(C('TABLE_HOLIDAY'));
		$result = $m->select();
		return $result;	
     }


    /*
     * 类型修改
     */
    public function editSaveType(){ 
    	if(IS_POST){
    		$fid = I('post.netid');
    		$netnm = I("post.net_name");
    		$m = D(C('TABLE_NETTYPE'));
    		$where['netname'] = $netnm;
    		$result = $m->where($where)->find();
    		if(!empty($result)){ 
    			$this->error('类型名称已存在,不要重复修改','',1);
    		}else{ 
				$m->netname = $netnm;
				$result = $m->where(array('netid'=>$fid))->save();
	    		if($result !== false){
	    			$this->success('编辑类型页面成功','typelist');
	    		}else{
	    			$this->error('更新数据失败','',1);
	    		}
			}

    	} 
    	
    }


     /*
     * 网络类型删除
     */
    public function deleType(){ 
    	$id = I('get.netid');
    	if(!empty($id)){
    		$m =  D(C('TABLE_NETTYPE'));
    		$where['netid'] = $id;
    		$result = $m->where($where)->delete();

    		if($result !== false){
    			$this->success('删除成功');
    		}else{
    			$this->error('删除失败','',1);
    		}
    	}else{
    		$this->error('参数错误','',1);
    	}

    }

	/*
     * 添加作品
     */
	public function	addProduct(){ 
		if(IS_POST){
			$atcobj = D(C('TABLE_ARTICLE'));
			$atcobj->title = I('post.title');
			$atcobj->fstvid = I('post.fstvid');
			$atcobj->netid = I('post.subordinateid');
			$atcobj->content = I('post.content');
			$atcobj->authornm = I('post.authornm');
			$atcobj->video = I('post.videourl');
			$atcobj->picture = "http://h5.hrtv.cn".I('post.picture');
			$atcobj->vocation = I('post.vocation');
			$atcobj->pubtime = I('post.pubtime');
			$atcobj->urladdr =  I('post.urladdr');
			$atcobj->summary = html_entity_decode(I('post.summary'));
			$atcobj->createdate = date('y-m-d h:i:s',time());
			$atcobj->updatedate = date('y-m-d h:i:s',time());
			$res = $atcobj->add();
		//	echo $fstvobj->getLastSql();die;
			if($res){
				$this->success('添加成功','main');
			}else{
				$this->error('添加失败','',1);
			}

		}else{ 
			$info = D(C('TABLE_HOLIDAY'));//节日名称
			$list = $info->select();
		    $this->assign('fstv',$list);

		    $m =  D(C('TABLE_NETTYPE'));//网络类型
		    $ls = $m->select();
		    $this->assign('nettype',$ls);
			$this->display();

		}

	}

	 /*
     * 跳转到作品修改页
     */
    public function editArtcle(){ 
	
	$id = I('get.id');
	if(!empty($id)){
			$re= D(C('TABLE_ARTICLE'));
		 	$where['id'] = $id;
			$result = $re->alias('a')->join('fvcn_holiday as h on h.fstvid = a.fstvid')->where($where)->find();
			$this->assign('artcrest',$result);
			$this->assign('fvcnm',$this->selectfvcnm());
			//$result = $re->alias('h')->join('fvcn_nettype as h on h.netid = a.netid')->where($where)->find();
			$this->assign('netid',$this->selectnetnm());
			$this->display();
		 }else{
		    $this->error('参数错误','',1);
		}	
    }


    /*
     * 作品修改
     */
    public function editSaveArticle(){ 
    	if(IS_POST){
    		$fid = I('post.id');
    		$atcobj = D(C('TABLE_ARTICLE'));
			$atcobj->title = I('post.title');
			$atcobj->fstvid = I('post.fstvid');
			$atcobj->netid = I('post.subordinateid');
			$atcobj->content = I('post.content');
            $atcobj->votes = I('post.votes');
			$atcobj->urladdr =  I('post.urladdr');
			$atcobj->authornm = I('post.authornm');
			$atcobj->video = I('post.videourl');
			$str1 = I('post.picture');
			$str2 = 'h5.hrtv.cn';
			if(strpos($str1,$str2) === false){ //使用绝对等于
    			//不包含
				$atcobj->picture = "http://".$_SERVER['HTTP_HOST'].I('post.picture');
			}else{
			    //包含
			    $atcobj->picture = I('post.picture');
			}
			$atcobj->vocation = I('post.vocation');
			$atcobj->summary = html_entity_decode(I('post.summary'));
			$atcobj->pubtime = date('y-m-d h:i:s',time());
			//$atcobj->updatedate = date('y-m-d h:i:s',time());
			$result = $atcobj->where(array('id'=>$fid))->save();
    		if($result !== false){
    			$this->success('更新数据成功', $_POST['httpref']); 
    			//$this->success('更新数据成功'.$atcobj->getLastInsID());
    		}else{
    			$this->error('更新数据失败','',1);
    		}
    	} 
    	
    }

	 /*
     * 作品删除
     */
    public function deleArtcle(){ 
    	$id = I('get.id');
    	if(!empty($id)){
    		$m =  D(C('TABLE_ARTICLE'));
    		$where['id'] = $id;
    		$result = $m->where($where)->delete();

    		if($result !== false){
    			$this->success('删除成功');
    		}else{
    			$this->error('删除失败','',1);
    		}
    	}else{
    		$this->error('参数错误','',1);
    	}

    }


	//根据节日id查询所属网络类型
/*	public function selType(){
		$result = array();
		$fstvid =$_POST['fstvid'];
		$result = D(C('TABLE_NETTYPE'))->where(array('fstvid'=>$fstvid))->field('netid,fstvid,netname')->order('fstvid asc')->select();
		$this->ajaxReturn($result,"JSON");
	}
	**/
	//查询网络类型
   public function selectnetnm(){ 
   		$m = D(C('TABLE_NETTYPE'));
		$result = $m->select();
		return $result;	
   }


   //评选专家
   public function addpeople(){ 

   	 if(IS_POST){
			$peoobj = D(C('TABLE_PEOPLE'));
			$peoobj->username = I('post.username');
			$peoobj->introduce = I('post.introduce');
			$peoobj->job = I('post.job');
			$peoobj->peopicture = "http://h5.hrtv.cn".I('post.peopicture');
			$peoobj->createdate = date('y-m-d h:i:s',time());
			$peoobj->updatedate = date('y-m-d h:i:s',time());
			$res = $peoobj->add();
			if($res){
				$this->success('添加成功','peopleList');
			}else{
				$this->error('添加失败','',1);
			}

		}else{ 
			$info = D(C('TABLE_PEOPLE'));//节日名称
			$list = $info->select();
		    $this->assign('people',$list);
			$this->display();
		}

   }

   //评选专家list
   public function peopleList(){ 
   	    $peobj = D(C('TABLE_PEOPLE'));
    	$count = $peobj->count();
    	//每页显示条数
    	$num = C('MAIN_NUM');
    	$page = new \Think\Page($count,$num);
    	$obj = pagestyle($page);
    	$show = $obj->show();
    	$result = $peobj->order(array('updatedate'=>'DESC'))->select();
    	$this->assign('page',$show);
    	$this->assign('peolist',$result);
    	$this->display();
   }
   
    /*
     * 跳转到作品修改页
     */
    public function editpeople(){ 
	 $userid = I('get.userid');
	if(!empty($userid)){
			$res= D(C('TABLE_PEOPLE'));
		 	$where['userid'] = $userid;
			$result = $res->where($where)->find();
			$this->assign('userobj',$result);
			$this->display();
		 }else{
		    $this->error('参数错误','',1);
		}	
    }


   //修改评选专家
   public function editSavepeople(){ 
   	 if(IS_POST){
    		$userid = I('post.userid');
    		$peobj = D(C('TABLE_PEOPLE'));
			$peobj->username = I('post.username');
			$peobj->job = I('post.job');
			$str1 = I('post.peopicture');
			$str2 = 'h5.hrtv.cn';
			if(strpos($str1,$str2) === false){ //使用绝对等于
    			//不包含
				$peobj->peopicture = "http://".$_SERVER['HTTP_HOST'].I('post.peopicture');
			}else{
			    //包含
			    $peobj->peopicture = I('post.peopicture');
			}
			$peobj->introduce = I('post.introduce');
			$peobj->createdate = date('y-m-d h:i:s',time());
			$peobj->updatedate = date('y-m-d h:i:s',time());
			$result = $peobj->where(array('userid'=>$userid))->save();
    		if($result !== false){
    			$this->success('更新数据成功','peopleList');
    		}else{
    			$this->error('更新数据失败','',1);
    		}
    	} 

   }

	 /*
     * 人物删除
     */
    public function delepeople(){ 
    	$userid = I('get.userid');
    	if(!empty($userid)){
    		$m =  D(C('TABLE_PEOPLE'));
    		$where['userid'] = $userid;
    		$result = $m->where($where)->delete();
    		if($result !== false){
    			$this->success('删除成功');
    		}else{
    			$this->error('删除失败','',1);
    		}
    	}else{
    		$this->error('参数错误','',1);
    	}

    }

    /*
     * 在线咨询
     */
    public function askList(){ 
    	$cons =  D(C('TABLE_CONSULTS'));
    	$ret  = $cons->select();
    	$this->assign('consults',$ret);
    	$this->display();
    }


}